﻿using Core;
using Core.ReadAndWrite;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsForms
{
    public partial class FormLopHoc : Form
    {
        protected int RowCuurent = 0;
        public FormLopHoc()
        {
            InitializeComponent();
            datagrlop.AutoGenerateColumns = false;
        }

        private void FormLopHoc_Load(object sender, EventArgs e)
        {

            LoadData();

        }

        private void LoadData()
        {
            List<LopHoc> ds = new List<LopHoc>();
            foreach (var item in QuanLyLop.Instance.data)
            {
                ds.Add(item as LopHoc);
            }
            datagrlop.DataSource = ds;
        }

        private void datagrlop_SelectionChanged(object sender, EventArgs e)
        {
            RowCuurent = datagrlop.CurrentRow.Index;
        }

        private void btndelete_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult kt = MessageBox.Show("Bạn muốn xóa không", "", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                if (kt == DialogResult.Yes)
                {
                    string id = datagrlop[0, RowCuurent].Value + "";
                    BaseData temp = QuanLyLop.Instance.Find(x => (x as LopHoc).MaLop == id);
                    QuanLyLop.Instance.Delete(temp);
                    DataSource.Instance.WriteTo(QuanLyLop.Instance.ToString(), "DsLop.txt");
                    LoadData();
                }


            }
            catch (Exception)
            {

                MessageBox.Show("lỗi không xóa được ");
            }
        }

        private void btnadd_Click(object sender, EventArgs e)
        {
            FormThemLop fr = new FormThemLop();
            fr.FormClosed += fr_FormClosed;
            fr.ShowDialog();
        }

        void fr_FormClosed(object sender, FormClosedEventArgs e)
        {
            LoadData();
        }

        private void undoToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            QuanLyLop.Instance.Undo();
            DataSource.Instance.WriteTo(QuanLyLop.Instance.ToString(), "DsLop.txt");
            LoadData();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnupdate_Click(object sender, EventArgs e)
        {
            FormThemLop frs = new FormThemLop();
            FormThemLop.malop = datagrlop[0, RowCuurent].Value+"";
            frs.FormClosed += frs_FormClosed;
            frs.ShowDialog();
        }

        void frs_FormClosed(object sender, FormClosedEventArgs e)
        {
            LoadData();
        }






    }
}
